﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Runtime.InteropServices;

namespace Ccit.Utils
{
    /// <summary>
    /// win32 api 接口类
    /// </summary>
    public class ApiLib
    {
        [DllImport("kernel32.dll")]
        private static extern int GetVolumeInformation(
        string lpRootPathName,
        string lpVolumeNameBuffer,
        int nVolumeNameSize,
        ref   int lpVolumeSerialNumber,
        int lpMaximumComponentLength,
        int lpFileSystemFlags,
        string lpFileSystemNameBuffer,
        int nFileSystemNameSize
        );

        /// <summary>
        /// 获取硬盘序列号
        /// </summary>
        /// <param name="drvID">磁盘序号</param>
        /// <returns>驱动器硬盘序列号</returns>
        public static string GetSerialNo(string drvID)
        {
            const int MAX_FILENAME_LEN = 256;
            int retVal = 0;
            int a = 0;
            int b = 0;
            string str1 = null;
            string str2 = null;


            int i = GetVolumeInformation(
            drvID + @":\",
            str1,
            MAX_FILENAME_LEN,
            ref   retVal,
            a,
            b,
            str2,
            MAX_FILENAME_LEN
            );

            return retVal.ToString("x");
        }

        /// <summary>
        /// 获取MAC地址列表
        /// </summary>
        /// <returns>MAC地址列表</returns>
        public static List<string> GetMacList()
        {
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            List<string> lstMac = new List<string>();

            //网卡数   
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo["IPEnabled"])
                {
                    lstMac.Add(mo["MacAddress"].ToString().Replace(":", ""));
                }
                mo.Dispose();
            }

            return lstMac;
        } 
    }
}
